﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbobsCOM;
using System.IO;

namespace CompanySyncProject
{
    public class Exporter
    {
        public static SAPbobsCOM.Company oCompany;
        public static System.IO.StreamWriter logtrace;

        public Exporter(SAPbobsCOM.Company cmp, System.IO.StreamWriter log)
        {
            oCompany = cmp;
            logtrace = log;
        }

        static void logTrace(string inputStr)
        {
            {
                logtrace.WriteLine(inputStr);
                logtrace.Flush();
            }
        }

        public static void Export(string folder)
        {
            ExportWarehouse(folder);
            ExportSalesPeople(folder);
            ExportPaymentTerms(folder);
            ExportShippingType(folder);
            ExportItem(folder);
            ExportBP(folder);
        }

        public static void ExportWarehouse(string folder)
        {
            List<string> warehouseList = new List<string>();
            SAPbobsCOM.Recordset oRec = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);

            const string queryStr = @"select WhsCode from [OWHS]";

            oRec.DoQuery(queryStr);
            string code = string.Empty;
            string errMsg = string.Empty;

            string path = String.Format(@"c:\temp\{0}\warehouse", folder);
            DirectoryInfo di = new DirectoryInfo(path);
            if (di.Exists == false)
                Directory.CreateDirectory(path);

            for (int i = 0; i < oRec.RecordCount; i++)
            {
                try
                {
                    code = oRec.Fields.Item(0).Value;
                    warehouseList.Add(code);
                    oRec.MoveNext();
                    SAPbobsCOM.IWarehouses owhs = (SAPbobsCOM.IWarehouses)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oWarehouses);
                    owhs.GetByKey(code);
                    string temp_string = String.Format(@"c:\temp\{0}\warehouse\warehouse{1}.xml", folder, code);
                    owhs.SaveXML(ref temp_string);
                    logTrace(String.Format(@"Export: Warehouse export succeeded. Code: {0}", code));
                }
                catch (Exception e)
                {
                    errMsg = oCompany.GetLastErrorDescription();
                    logTrace(String.Format(@"Export: Warehouse export failed. Code: {0}, Reason: {1}", code, errMsg));
                }
            }
        }


        public static void ExportItem(string folder)
        {
            List<string> itemList = new List<string>();
            SAPbobsCOM.Recordset oRec = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);

            const string queryStr = @"select itemCode from [OITM]";

            oRec.DoQuery(queryStr);
            string code = string.Empty;
            string errMsg = string.Empty;

            string path = String.Format(@"c:\temp\{0}\item", folder);
            DirectoryInfo di = new DirectoryInfo(path);
            if (di.Exists == false)
                Directory.CreateDirectory(path);

            for (int i = 0; i < oRec.RecordCount; i++)
            {
                try
                {
                    code = oRec.Fields.Item(0).Value;
                    itemList.Add(code);
                    oRec.MoveNext();
                    SAPbobsCOM.IItems oitms = (SAPbobsCOM.IItems)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems);
                    oitms.GetByKey(code);
                    string temp_string = String.Format(@"c:\temp\{0}\item\item{1}.xml", folder, code);
                    oitms.SaveXML(ref temp_string);
                    logTrace(String.Format(@"Export: item export succeeded. Code: {0}", code));
                }
                catch (Exception)
                {
                    errMsg = oCompany.GetLastErrorDescription();
                    logTrace(String.Format(@"Export: item export failed. Code: {0}, Reason: {1}", code, errMsg));
                }
            }
        }

        public static void ExportShippingType(string folder)
        {
            List<string> shtyList = new List<string>();
            SAPbobsCOM.Recordset oRec = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);

            const string queryStr = @"select trnspCode from [OSHP]";

            oRec.DoQuery(queryStr);
            string code = string.Empty;
            string errMsg = string.Empty;

            string path = String.Format(@"c:\temp\{0}\ShippingType", folder);
            DirectoryInfo di = new DirectoryInfo(path);
            if (di.Exists == false)
                Directory.CreateDirectory(path);

            for (int i = 0; i < oRec.RecordCount; i++)
            {
                try
                {
                    code = oRec.Fields.Item(0).Value.ToString();
                    shtyList.Add(code);
                    oRec.MoveNext();
                    SAPbobsCOM.IShippingTypes oshty = (SAPbobsCOM.IShippingTypes)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oShippingTypes);
                    oshty.GetByKey(int.Parse(code));
                    string temp_string = String.Format(@"c:\temp\{0}\shippingtype\shippingtype{1}.xml", folder, code);
                    oshty.SaveXML(ref temp_string);
                    logTrace(String.Format(@"Export: shipping type export succeeded. Code: {0}", code));
                }
                catch (Exception)
                {
                    errMsg = oCompany.GetLastErrorDescription();
                    logTrace(String.Format(@"Export: shipping type export failed. Code: {0}, Reason: {1}", code, errMsg));
                }
            }
        }

        public static void ExportBP(string folder)
        {
            List<string> BPList = new List<string>();
            SAPbobsCOM.Recordset oRec = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);

            const string queryStr = @"select CardCode from [OCRD]";

            oRec.DoQuery(queryStr);
            string code = string.Empty;
            string errMsg = string.Empty;

            string path = String.Format(@"c:\temp\{0}\BP", folder);
            DirectoryInfo di = new DirectoryInfo(path);
            if (di.Exists == false)
                Directory.CreateDirectory(path);

            for (int i = 0; i < oRec.RecordCount; i++)
            {
                try
                {
                    code = oRec.Fields.Item(0).Value;
                    BPList.Add(code);
                    oRec.MoveNext();
                    SAPbobsCOM.IBusinessPartners ocrd = (SAPbobsCOM.IBusinessPartners)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oBusinessPartners);
                    ocrd.GetByKey(code);
                    string temp_string = String.Format(@"c:\temp\{0}\BP\BP{1}.xml", folder, code);
                    ocrd.SaveXML(ref temp_string);
                    logTrace(String.Format(@"Export: BP export succeeded. Code: {0}", code));
                }
                catch (Exception)
                {
                    errMsg = oCompany.GetLastErrorDescription();
                    logTrace(String.Format(@"Export: BP export failed. Code: {0}, Reason: {1}", code, errMsg));
                }
            }
        }

        public static void ExportPaymentTerms(string folder)
        {
            List<int> BPList = new List<int>();
            SAPbobsCOM.Recordset oRec = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);

            const string queryStr = @"select GroupNum from [OCTG] where groupnum > 0";

            oRec.DoQuery(queryStr);
            int code = 0;
            string errMsg = string.Empty;

            string path = String.Format(@"c:\temp\{0}\PaymentTerms", folder);
            DirectoryInfo di = new DirectoryInfo(path);
            if (di.Exists == false)
                Directory.CreateDirectory(path);

            for (int i = 0; i < oRec.RecordCount; i++)
            {
                try
                {
                    code = oRec.Fields.Item(0).Value;
                    BPList.Add(code);
                    oRec.MoveNext();
                    SAPbobsCOM.IPaymentTermsTypes oslp = (SAPbobsCOM.IPaymentTermsTypes)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oPaymentTermsTypes);
                    oslp.GetByKey(code);
                    string temp_string = String.Format(@"c:\temp\{0}\PaymentTerms\PaymentTerms{1}.xml", folder, code);
                    oslp.SaveXML(ref temp_string);
                    logTrace(String.Format(@"Export: PaymentTerms export succeeded. Code: {0}", code));
                }
                catch (Exception)
                {
                    errMsg = oCompany.GetLastErrorDescription();
                    logTrace(String.Format(@"Export: PaymentTerms export failed. Code: {0}, Reason: {1}", code, errMsg));
                }
            }
        }

        public static void ExportSalesPeople(string folder)
        {
            List<int> BPList = new List<int>();
            SAPbobsCOM.Recordset oRec = (SAPbobsCOM.Recordset)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);

            const string queryStr = @"select slpCode from [OSLP] where slpCode > 0";

            oRec.DoQuery(queryStr);
            int code = 0;
            string errMsg = string.Empty;

            string path = String.Format(@"c:\temp\{0}\SalesPeople", folder);
            DirectoryInfo di = new DirectoryInfo(path);
            if (di.Exists == false)
                Directory.CreateDirectory(path);

            for (int i = 0; i < oRec.RecordCount; i++)
            {
                try
                {
                    code = oRec.Fields.Item(0).Value;
                    BPList.Add(code);
                    oRec.MoveNext();
                    SAPbobsCOM.ISalesPersons oslp = (SAPbobsCOM.ISalesPersons)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oSalesPersons);
                    oslp.GetByKey(code);
                    string temp_string = String.Format(@"c:\temp\{0}\SalesPeople\SalesPeople{1}.xml", folder, code);
                    oslp.SaveXML(ref temp_string);
                    logTrace(String.Format(@"Export: SalesPeople export succeeded. Code: {0}", code));
                }
                catch (Exception)
                {
                    errMsg = oCompany.GetLastErrorDescription();
                    logTrace(String.Format(@"Export: SalesPeople export failed. Code: {0}, Reason: {1}", code, errMsg));
                }
            }
        }
    }
}
